package top.chmin.project.cloud.sys.relate.info;

import java.util.List;
import java.time.LocalDateTime;
import top.chmin.project.cloud.tool.DatetimeTool;
import top.chmin.project.cloud.tool.LocalTool;
import top.chmin.project.cloud.common.Page;
import top.chmin.project.cloud.common.BasePlugin;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.json.JSONUtil;



/**
 * chmin
 * 2024-01-26 14:02:31
 * cloud-code
 */
@Slf4j
@Component
public class RelateInfoPlugin extends BasePlugin {

    public void checkSave(RelateInfoSaveRest.P p) {
        log.debug("checkSave-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("保存对象不能为空");
        }
    }

    public void beforeSave(RelateInfo relateInfo) {
        log.debug("beforeSave-relateInfo: {}", JSONUtil.toJsonStr(relateInfo));
        /* append $pluginCodeAppender$ */

        relateInfo.setCreateBy(LocalTool.getUserId());
        relateInfo.setUpdateBy(LocalTool.getUserId());
        relateInfo.setCreateTime(LocalDateTime.now());
        relateInfo.setUpdateTime(LocalDateTime.now());

    }

    public void afterSave(RelateInfoSaveRest.P p, RelateInfo relateInfo, int result) {
        log.debug("afterSave-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterSave-relateInfo: {}", JSONUtil.toJsonStr(relateInfo));
        log.debug("afterSave-result: {}", JSONUtil.toJsonStr(result));

        relateInfo.setUpdateBy(LocalTool.getUserId());
        relateInfo.setUpdateTime(LocalDateTime.now());

    }

    public void checkUpdate(RelateInfoUpdateRest.P p) {
        log.debug("checkUpdate-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("更新对象不能为空");
        }
        if (p.getId() == null) {
            throw new RuntimeException("更新对象id不能为空");
        }
    }

    public void beforeUpdate(RelateInfo relateInfo) {
        log.debug("beforeUpdate-relateInfo: {}", JSONUtil.toJsonStr(relateInfo));

    }

    public void afterUpdate(RelateInfoUpdateRest.P p, RelateInfo relateInfo, int result) {
        log.debug("afterUpdate-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterUpdate-relateInfo: {}", JSONUtil.toJsonStr(relateInfo));
        log.debug("afterUpdate-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkDelete(RelateInfoDeleteRest.P p) {
        log.debug("checkDelete-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("删除对象不能为空");
        }
        if (p.getId() == null) {
            throw new RuntimeException("删除对象id不能为空");
        }
    }

    public void beforeDelete(RelateInfo relateInfo) {
        log.debug("beforeDelete-relateInfo: {}", JSONUtil.toJsonStr(relateInfo));

    }

    public void afterDelete(RelateInfoDeleteRest.P p, RelateInfo relateInfo, int result) {
        log.debug("afterDelete-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterDelete-relateInfo: {}", JSONUtil.toJsonStr(relateInfo));
        log.debug("afterDelete-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelect(RelateInfoSelectRest.P p) {
        log.debug("checkSelect-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("查询对象不能为空");
        }
        if (p.getId() == null) {
            throw new RuntimeException("查询对象id不能为空");
        }
    }

    public void beforeSelect(RelateInfoSelectRest.P p, RelateInfoCondition c) {
        log.debug("beforeSelect-p: {}", JSONUtil.toJsonStr(p));
        log.debug("beforeSelect-c: {}", JSONUtil.toJsonStr(c));

    }

    public void afterSelect(RelateInfoSelectRest.P p, RelateInfoSelectRest.R r) {
        log.debug("afterSelect-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterSelect-result: {}", JSONUtil.toJsonStr(r));

    }

    public void checkPage(RelateInfoPageRest.P p) {
        log.debug("checkPage-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("分页对象不能为空");
        }
    }

    public void beforePage(RelateInfoPageRest.P p, RelateInfoCondition c) {
        log.debug("beforePage-p: {}", JSONUtil.toJsonStr(p));
        log.debug("beforePage-c: {}", JSONUtil.toJsonStr(c));

        /*###########auto#start###########*/
        c.columnCode().eq(p.getColumnCode());
        c.createBy().eq(p.getCreateBy());
        c.createTime().ge(p.getCreateTime() == null || p.getCreateTime().length == 0 ? null : p.getCreateTime()[0]);
        c.createTime().lt(p.getCreateTime() == null || p.getCreateTime().length < 2 ? null : DatetimeTool.nextDay(p.getCreateTime()[1]));
        c.sourceColumnCode().eq(p.getSourceColumnCode());
        c.updateBy().eq(p.getUpdateBy());
        c.updateTime().ge(p.getUpdateTime() == null || p.getUpdateTime().length == 0 ? null : p.getUpdateTime()[0]);
        c.updateTime().lt(p.getUpdateTime() == null || p.getUpdateTime().length < 2 ? null : DatetimeTool.nextDay(p.getUpdateTime()[1]));
        /*###########auto##end############*/
        /* append $pluginBeforePageAppender$ */

    }

    public void afterPage(RelateInfoPageRest.P p, Page<RelateInfoPageRest.R> r) {
        log.debug("afterPage-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterPage-result: {}", JSONUtil.toJsonStr(r));

    }
    /* append $pluginSelectOptionMethodAppender$ */
}
